<?php

namespace JLPay\Models\Openmerch\Query;

use JLPay\Models\BaseResponse;

/**
 * 商户渠道信息查询响应
 */
class MerchChannelInfoQueryResponse extends BaseResponse
{
    protected string $merchNo = '';  // 商户号
    protected string $merchName = '';  // 商户名称
    protected array $channelInfo = [];  // 渠道信息

    /**
     * 构造函数
     *
     * @param array|null $responseArray
     */
    public function __construct(?array $responseArray = null)
    {
        parent::__construct($responseArray);
        
        if ($responseArray) {
            $this->merchNo = $responseArray['merch_no'] ?? '';
            $this->merchName = $responseArray['merch_name'] ?? '';
            $this->channelInfo = [];
            
            // 处理渠道信息列表
            $channelInfoList = $responseArray['channel_info'] ?? [];
            if ($channelInfoList) {
                foreach ($channelInfoList as $channelData) {
                    if (is_array($channelData)) {
                        $channelInfo = new ChannelInfo();
                        $channelInfo->setChannelId($channelData['channel_id'] ?? '');
                        $channelInfo->setReportStatus($channelData['report_status'] ?? '');
                        $channelInfo->setChannelMerchNo($channelData['channel_merch_no'] ?? '');
                        $channelInfo->setFailReason($channelData['fail_reason'] ?? '');
                        $channelInfo->setChannelMerchName($channelData['channel_merch_name'] ?? '');
                        $channelInfo->setChannelMerchShortname($channelData['channel_merch_shortname'] ?? '');
                        $channelInfo->setReportTime($channelData['report_time'] ?? '');
                        $channelInfo->setChannelOrgId($channelData['channel_org_id'] ?? '');
                        $channelInfo->setFeeType($channelData['fee_type'] ?? '');
                        $this->channelInfo[] = $channelInfo;
                    }
                }
            }
        }
    }

    /**
     * 获取商户号
     *
     * @return string
     */
    public function getMerchNo(): string
    {
        return $this->merchNo;
    }

    /**
     * 获取商户名称
     *
     * @return string
     */
    public function getMerchName(): string
    {
        return $this->merchName;
    }

    /**
     * 获取渠道信息
     *
     * @return array
     */
    public function getChannelInfo(): array
    {
        return $this->channelInfo;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        $data['merch_no'] = $this->merchNo;
        $data['merch_name'] = $this->merchName;
        $data['channel_info'] = array_map(function($channel) {
            return $channel->toArray();
        }, $this->channelInfo);
        return $data;
    }
} 